package org.friut.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * File utils
 * 
 * @author Tang
 */
public enum FileUtils {
	
	INSTANCE;
	
	private static final Logger LOG = LoggerFactory.getLogger(FileUtils.class);
	
	/**
	 * Get extension
	 * 
	 * @param file
	 * @return
	 */
	public String getExtension(File file) {
		return (file != null) ? getExtension(file.getName()) : "";
	}
	
	/**
	 * Get extension
	 * 
	 * @param file
	 * @return
	 */
	public String getExtension(String fileName) {
		return getExtension(fileName, "");
	}
	
	/**
	 * Get extension
	 * 
	 * @param file
	 * @return
	 */
	public String getExtension(String fileName, String ext) {
		if (fileName != null && fileName.length() > 0) {
			int i = fileName.lastIndexOf(".");
			if (i > -1 && i < (fileName.length() - 1)) {
				return fileName.substring(i + 1);
			}
		}
		return ext;
	}
	
	/**
	 * Get extension
	 * 
	 * @param file
	 * @return
	 */
	public String trimExtension(String fileName) {
		if (fileName != null && fileName.length() > 0) {
			int i = fileName.lastIndexOf(".");
			if (i > -1 && i < fileName.length()) {
				return fileName.substring(0, i);
			}
		}
		return fileName;
	}
	
	/**
	 * Make file directory
	 * 
	 * @param dir
	 * @return
	 */
	public String mkdir(String dir) {
		boolean created = false;
		File dirFile = new File(dir);
		if (!dirFile.exists()) {
			if (dirFile.mkdirs()) {
				created = true;
			} else {
				LOG.warn("Make dir failed. Location: {}", dir);
			}
		} else {
			created = true;
		}
		
		return created ? dir : "";
	}
	
	/**
	 * Write file
	 * 
	 * @param filePath
	 * @param content
	 * @param append
	 */
	public void writeFile(String filePath, String content, boolean append) {
		try {
			StringBuffer sb = new StringBuffer();
			BufferedWriter bw = new BufferedWriter(new FileWriter(filePath, append));
			
			sb.append(content);
			// sb.append("\r\n");
			
			bw.write(sb.toString());
			bw.flush();
			bw.close();
		} catch (IOException ioe) {
			LOG.error("File write failed.", ioe);
		}
	}
	
	/**
	 * Read file by filePath
	 * 
	 * @param filePath
	 * @return
	 */
	public String readFile(String filePath) {
		File file = new File(filePath);
		StringBuilder sb = new StringBuilder();
		try {
			FileInputStream fis = new FileInputStream(file);
			InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
			BufferedReader reader = new BufferedReader(isr);
			
			String tempString = null;
			while ((tempString = reader.readLine()) != null) {
				sb.append(tempString);
			}
			reader.close();
		} catch (IOException e) {
			LOG.error("Read write failed.", e);
		}
		return sb.toString();
	}
}
